<?php
/* 
 *供方付款（采购单）
 */
class PaymentAction extends TransAction {
    public function _filter(&$map) {
        $key = trim($_REQUEST['keywords']);
        if(!empty($key)) {
            $where['Product.name'] = array('like',"%".$key."%");//产品名称
            $where['Product.ename'] = array('like',"%".$key."%");
            $where['Product.cas'] = array('like',"%".$key."%");
            $where['Buy.mano'] = array('like',"%".$key."%");
            //$where['Order_detail.mano'] = array('like',"%".$key."%"); //仅搜索销售分单号即可
            //$where['Sale.po'] = array('like',"%".$key."%");
            $where['Supplier.name'] = array('like',"%".$key."%");
            $where['_logic'] = "or";
            
        }
        if(!empty($_REQUEST['sale_key']) ){
            $skey = trim($_REQUEST['sale_key']);
            $where['Order_detail.mano'] = array('like',"%".$skey."%"); //仅搜索销售分单号即可
            $where['Sale.po'] = array('like',"%".$skey."%");
            $where['_logic'] = "or";
        }
        if(is_array($where))
            $map['_complex'] = $where;

        if($_REQUEST['status'] != '' )
            $map['status'] = $_REQUEST['status'];
        if($_REQUEST['dstatus'] != '' )
            $map['Order_detail.status'] = $_REQUEST['dstatus'];
        if( $_REQUEST['in_qc_status'] != '' )
            $map['in_qc_status'] = $_REQUEST['in_qc_status'];
        if( $_REQUEST['sample_qc_status'] != '' )
            $map['sample_qc_status'] = $_REQUEST['sample_qc_status'];
        if( $_REQUEST['pay_status'] != '' )
            $map['pay_status'] = $_REQUEST['pay_status'];

        //time 
        $cond_time = array();
        if( $_REQUEST['start_time'] != '' )
          array_push($cond_time, array( 'egt' , strtotime($_REQUEST['start_time']) ) );
          //$cond_time = array( 'egt' , strtotime($_REQUEST['start_time']) );
        if( $_REQUEST['end_time'] != '' )
          array_push($cond_time, array( 'elt' , strtotime($_REQUEST['end_time']) ) );

        //选择了时间类型,并且时间不为空，则按时间进行搜索
        if( $_REQUEST['time_type'] != "" && 
          ($_REQUEST['end_time'] != '' ||  $_REQUEST['start_time'] != '' ) ){
            switch ($_REQUEST['time_type']) {
              case '1': 
                $map['first_pay_time'] = $cond_time;
                break;
              case '2': 
                $map['remain_pay_time'] = $cond_time;
                break;
              case '3': 
                $map['back_money_time'] = $cond_time;
                break;
              case '4': //交货期
                $map['Order_detail.delivery_date '] = $cond_time;
                break;
              case '5': //创建时间
                $map['create_time '] = $cond_time;
                break;
              
              default:  //按下单时间查询
                $map['first_pay_time '] = $cond_time;
                break;
            }
            
        }

    }

    public function  index() {
        if(empty($_REQUEST['sale_key']))
            parent::index('BuySPView');
        else if(!empty($_REQUEST['dstatus']) || !empty($_REQUEST['sale_key']))
            parent::index('BuyOrderView');
        else
            parent::index('Buy');
    }

    public function edit(){
        $model = M ( 'Buy');
        $id = $_REQUEST [$model->getPk ()];
        if(isset($id)){
            $vo = $model->where('id = '.$id )->find();
            $this->assign ( 'vo', $vo );
            $this->display ();
        }
    }

    public function check(){
        $model = M ( 'Buy');
        $id = $_REQUEST [$model->getPk ()];
        if(isset($id)){
            $vo = $model->where('id = '.$id )->find();
            $this->assign ( 'vo', $vo );
            $this->display ();
        }
    }

    public function update()
    {
        A('Buy')->update();
    }

    public function reset_pay(){
        M('Buy')->where('id = '.$_REQUEST['id'])->setField($_REQUEST['field'],0);
        $this->success("财务值重新归为0！");
    }
     //等待退款
     public function reback_money(){
        M('Buy')->where('id = '.$_REQUEST['id'])->setField($_REQUEST['field'],3);
        $this->success("成功设采购单为等待退款中！");
    }

}
?>
